<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Common\Library\COM;

/**
 * Description of Member
 *
 * @author gangguo
 */
class  MemberDo {
    //put your code here

    public static function getObject(){
        return new \Common\Model\MembersModel();
    }


    public static function registMember($data){
        $member_id = self::getObject()->SaveData($data);
        \Think\Log::write('$member_id:::'.var_export($member_id,1));
        if($member_id > 0){
            $pam = array(
                'member_id'=>$member_id,
                'createtime'=>  time(),
                'login_account'=>$data['nickname'],
                'openid'=>$data['openid']
            );
            return D('MemberPam')->SaveData($pam);
        }
    }


    public static function newPassword($member_id,$pass){
        if($member_id > 0 && $pass){
                $password_account = getRandStr(10);
                $login_password = genSecretStr($pass, $password_account);
                $Pass = new \Common\Model\MemberPamModel();
                $data = array(
                    'password_account'=>$password_account,
                    'login_password'=>$login_password
                );
                return $Pass->where("`member_id` = '{$member_id}'")->save($data);
        }
        return FALSE;

    }

    //检测用户名有没有注册
    public static function checkUsername($login_account){
        if(isUsername($login_account)){
            $Pass = new \Common\Model\MemberPamModel();
            if($info = $Pass->login($login_account)){
                return $info;
            }
        }
        return FALSE;
    }

    /**
     * 获取登陆信息
     */
    public static function getLoginInfo($member_id=0,$type=''){
        if($type == 'login'){
            self::getObject()->updateField('last_login',time(),$member_id);
        }
        return self::getObject()->getDetailInfo($member_id);
    }

    //添加会员积分
    public static function addMemberPoint($member_id,$point,$remark){
        if($member_id > 0 && is_numeric($point)){
            $Point = new \Common\Model\MemberPointModel();
            $p_total = $Point->getMemberPoint($member_id);
            $data = array('point'=>$p_total+$point);
            if($point > 0){
                self::getObject()->where("`member_id`='{$member_id}'")->setInc('point',$point);// 用户的积分增加
            }else{
                self::getObject()->where("`member_id`='{$member_id}'")->setDec('point',(0-$point));//用户积分减少
            }
            $data = array(
                'member_id'=>$member_id,
                'point'=>$p_total+$point,
                'addtime'=>time(),
                'change_point'=>$point,
                'reason'=>$remark
                );
            $Point->add($data);
        }
        return FALSE;
    }

}
